package com.swipemaster.activities;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.GridLayoutAnimationController;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.google.ads.AdView;
import com.google.analytics.tracking.android.EasyTracker;
import com.swipemaster.R;
import com.swipemaster.common.AdMobListener;
import com.swipemaster.common.Global;
import com.swipemaster.dialogs.CustomToast;
import com.swipemaster.game.modes.LevelsMode;
import com.swipemaster.levels.levelinfo.LevelInfo;
import com.swipemaster.record.RecordsManager;

/** Activity with level selector. */
public class PlayLevelsActivity extends CommonActivity
{
	public static final int ANIM_TIME = 1100;
	private static final int LEVELS_ABOVE_SECETION = 7;

	public final Handler handler = new Handler();
	private GridView gridView;
	private Button backButton;
	private int lastSelectedLevel;
	private boolean backAlreadyPressed = false;
	private CustomToast customToast;
	private TextView starsView;

	public void launchDisappearAnimation()
	{
		setAnimation(true);
		handler.postDelayed(new Runnable()
		{
			@Override
			public void run()
			{
				for (int i = 0; i < gridView.getChildCount(); i++)
				{
					gridView.getChildAt(i).setVisibility(View.INVISIBLE);
				}
			}
		}, 200);
	}

	@Override
	public void onBackPressed()
	{
		if (backAlreadyPressed)
		{
			return;
		}
		backAlreadyPressed = true;

		setAnimation(true);
		handler.postDelayed(new Runnable()
		{
			@Override
			public void run()
			{
				for (int i = 0; i < gridView.getChildCount(); i++)
				{
					gridView.getChildAt(i).setVisibility(View.INVISIBLE);
				}
			}
		}, 200);
		handler.postDelayed(new Runnable()
		{
			@Override
			public void run()
			{
				callSuperOnBackPressed();
			}
		}, ANIM_TIME);
	}

	@SuppressLint("ShowToast")
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		setContentView(R.layout.play_levels);
		EasyTracker.getInstance().activityStart(this);
		overridePendingTransition(0, R.anim.fade_out);

		gridView = (GridView) findViewById(R.id.grid_view);
		backButton = (Button) findViewById(R.id.back_level_page);
		starsView = (TextView) findViewById(R.id.level_page_stars);
		lastSelectedLevel = 0;

		final AdView banner = (AdView) findViewById(R.id.play_levels_banner);
		banner.setAdListener(Global.app.adMobListener);
		Global.app.adMobListener.hashToNameMap.put(banner.hashCode(), AdMobListener.AD_4);

		setAdapter();

		customToast = new CustomToast(this);

		backButton.setTypeface(Global.app.consts.font);
		backButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				onBackPressed();
			}
		});

		gridView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, final int position, long id)
			{
				final LevelInfo levelInfo = RecordsManager.getInfo(position);
				lastSelectedLevel = position;

				final boolean isFirstCompleted = RecordsManager.getInfo(0).isCompleted;
				final boolean isSecondCompleted = RecordsManager.getInfo(1).isCompleted;

				if (levelInfo.isLocked)
				{
					if (!isFirstCompleted)
					{
						customToast.showCompleteLevel(0);
					}
					else if (!isSecondCompleted)
					{
						customToast.showCompleteLevel(1);
					}
					else
					{
						customToast.showYouNeedStars(RecordsManager.getStarsForLevel(position));
					}
				}
				else
				{
					startLevel(position);
				}
			}
		});

		setAnimation(false);

		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onStop()
	{
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	/** Set new adapter to grid view, which will cause a refresh of all the locks images etc. */
	public void setAdapter()
	{
		gridView.setAdapter(new LevelSelectorAdapter(this));
		setGridSelection();
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		setAdapter();
		setStarsView();
	}

	private void setStarsView()
	{
		final int stars = RecordsManager.getStars();
		starsView.setText(Integer.toString(stars));
	}

	private void callSuperOnBackPressed()
	{
		super.onBackPressed();
		overridePendingTransition(0, 0);
	}

	private void setAnimation(boolean isDisappear)
	{
		Animation anim;
		final GridLayoutAnimationController glac;
		if (isDisappear)
		{
			anim = AnimationUtils.loadAnimation(getBaseContext(), R.anim.level_disappear_anim);
			glac = new GridLayoutAnimationController(anim, 0.5f, 0.5f);
			glac.setDirection(GridLayoutAnimationController.DIRECTION_BOTTOM_TO_TOP
					| GridLayoutAnimationController.DIRECTION_RIGHT_TO_LEFT);
		}
		else
		{
			anim = AnimationUtils.loadAnimation(getBaseContext(), R.anim.level_anim);
			glac = new GridLayoutAnimationController(anim, 0.5f, 0.5f);
		}
		gridView.setAnimationCacheEnabled(false);
		gridView.invalidate();
		gridView.setLayoutAnimation(glac);
	}

	/** scrolls grid to approximately previous position when coming back from game activity */
	private void setGridSelection()
	{
		if (lastSelectedLevel > LEVELS_ABOVE_SECETION)
		{
			gridView.setSelection(lastSelectedLevel - LEVELS_ABOVE_SECETION - 1);
		}
	}

	private void startLevel(int levelIndex)
	{
		lastSelectedLevel = levelIndex;
		Global.app.gameMode = new LevelsMode(levelIndex);
		final Intent intent = new Intent(PlayLevelsActivity.this, GameActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
	}
}